package com.bjsxt.userscenter.common.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 模拟浏览器请求任何一个网址
 * 
 * @author WangshSxt
 *
 */
public class HttpUtil
{
	/**
	 * 输入一个url,
	 * 返回这个url对应的html代码
	 * 
	 * @param url:网址
	 * @return 返回网址对应的html代码
	 */
	public String methodGet(String urlStr)
	{
		//ConstatFinalUtil.SYS_LOGGER.info("--methodGet--url:{}",urlStr);
		StringBuffer sb = new StringBuffer() ; 
		try
		{
			URL url = new URL(urlStr);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection() ; 
			
			/* 获取网址对应的输入流和输出流 */
			InputStream is = connection.getInputStream() ;
			
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			
			String line = "" ; 
			while((line = br.readLine()) != null)
			{
				sb.append(line);
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return sb.toString() ; 
	}
	
	public static void main(String[] args)
	{
		HttpUtil httpUtil = new HttpUtil() ;
		
		String url = "http://localhost:8080/userscenter-back/outer/json/verfiyToken.htm?json={%22version%22:%221%22,%22data%22:{%22token%22:%2226174d6f-2cac-4a00-99ff-7d79df12f38a%22}}" ; 
		String res = httpUtil.methodGet(url);
		System.out.println(res);
	}
}
